﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

using Game.Common;
using Game.Map;
using Game.Objects;
using Game.Surface2;
using Game.SurfaceControl2;
using Game.Toolkit;

namespace GameEditor2.Toolbox
{
    public partial class BuildingItemControl : UserControl
    {
        GameObjectGroup _GameObjectGroup;
        SurfaceControl2 _SurfaceControl;
        
        public BuildingItemControl()
        {
            InitializeComponent();
            
            IGameMap map = Game.Map.Factory.Create(
                4, 4, StandardTerrain.Grass);
            _SurfaceControl = new SurfaceControl2(map);
            _SurfaceControl.GameScale = 0.50;
            this.canvas2.Children.Add(_SurfaceControl);
        }
        
        public GameObjectGroup GameObjectGroup
        {
            get { return _GameObjectGroup; }
            set
            {
                _GameObjectGroup = value;
                DisplayType();
            }
        }
        
        private void OnClick(object source, EventArgs e)
        {
            if (_GameObjectGroup != null)
            {
                SelectedBuilding.GameObjectGroup = _GameObjectGroup;
            }
            
            SelectedGameObjectGroup.GameObjectGroup = null;
        }
        
        void DisplayType()
        {
            if (_GameObjectGroup != null)
            {
                DisplayBuildingObject();
                base.InvalidateVisual();
            }
        }
        
        void DisplayBuildingObject()
        {
            List<GameObjectGroupPart> parts = _GameObjectGroup.Parts;
            
            Game.Common.Point center = new Game.Common.Point(0, 0);
            IMapField field = _SurfaceControl.GameMapView.Fields[
                new Game.Common.Point(0, 0)];
            
            foreach(GameObjectGroupPart part in parts)
            {
                AssignObject(
                    (int)part.GetPoint(center).X,
                    (int)part.GetPoint(center).Y,
                    part.ObjectType, 
                    part.GameDirection,
                    new System.Windows.Point(0, 0),
                    _SurfaceControl);
            }
        }
        
        static void AssignObject(int x, 
                                 int y,
                                 ObjectType item,
                                 GameDirection offset,
                                 System.Windows.Point location,
                                 SurfaceControl2 surfaceControl)
        {
            double dx = location.X;
            double dy = location.Y;
            dx += offset.Value.X;
            dy += offset.Value.Y;
            
            if ((dx % 2) == 1)
            {
                dy += 1;
            }
            
            Game.Common.Point origin = new Game.Common.Point(dx, dy);
            IMapField field = surfaceControl.GameMapView.GetField(
                origin, ObjectOrientation.Center);
            
            if (field != null) 
            {
                field.MapObject = new GameObject(item);
            }
        }
        
        void Button_Click(object sender, RoutedEventArgs e)
        {
            SelectedBuilding.GameObjectGroup = _GameObjectGroup;
        }
    }
}